"""
DataProcess -->  数据处理
    |
ComponentManager  -->  生成管理
    |
ManageEvent   -->  事件管理
"""
import json
import settings
import os
import midd_Enum
"""
{
'button': '左键', 
'behavior': '点击', 
'count': '1', 
'scroll': None, 
'start': [397, 28], 
'end': None, 
'key': None, 
'time': None, 
'key_string': None, 
'key_group': None}
"""

# 数据处理对象
class DataProcess:
    def __init__(self,file_path):
        self.file_path = file_path                 # 文件路径
        self.data = []
        self.read()

    def __call__(self):
        return self.data
    
    def read(self):
        """读取数据"""
        with open(self.file_path,'r',encoding='utf-8') as f:
            data = json.load(f)
        self.process(data)

    def safe_int_convert(self, value, default=None):
        """安全地将值转换为整数"""
        try:
            return int(value)
        except (ValueError, TypeError):
            return default

    def list_to_tuple(self, lst):
        """将列表转换为元组"""
        if lst:
            return tuple(lst)
        return None

    def process_scale_data(self, data):
        """处理缩放数据"""
        if data:
            x = data[0]
            y = data[1]
            proportion = 100/settings.PRORORTION_SIZE
            print('缩放比例：',proportion)
            x = x * proportion
            y = y * proportion
            data = (x,y)
            return data
        else:
            return None
        
    def process(self, data):
        """数据分步处理"""
        # 遍历数据列表中的字典并单独处理
        for i in data:
            i['button'] = midd_Enum.Button_Dict_Type[i['button']]
            i['behavior'] = i['behavior']
            i['count'] = self.safe_int_convert(i['count'])
            i['scroll'] = self.list_to_tuple(i['scroll'])
            i['start'] = self.process_scale_data(i['start'])
            i['end'] = self.process_scale_data(i['end'])
            i['key'] = i['key']
            i['time'] = self.safe_int_convert(i['time'])
            i['key_string'] = i['key_string']
            i['key_group'] = i['key_group']
        self.data = data
        print('数据处理成功')


if __name__ == '__main__':
    file_path = os.path.join(settings.USER_PATH,'测试数据.json')
    data_process = DataProcess(file_path)
    print(data_process())